"""
@author: 江同学呀
@file: histogram_field.py
@date: 2024/7/27 21:38
@desc:
    https://www.elastic.co/guide/en/elasticsearch/reference/7.17/histogram.html

    Pre-aggregated numerical values in the form of a histogram.
    以直方图形式预聚合的数值。
"""
from abc import ABC
from typing import Type, Union, Dict

from espc.common.field_common import TimeSeriesMetric
from espc.orm.model.base.base import _Base
from espc.orm.model.struct.fields import Histogram
from espc.orm.model.mapping.field.base_field.base_field import _BaseField
from espc.orm.model.mapping.field.field_attribute import TimeSeriesMetricAttribute
from espc.utils.build_add_attr import build_add_attr


class HistogramField(_BaseField, TimeSeriesMetricAttribute, ABC):
    """
    直方图 字段
    """
    _field_type: str = "histogram"

    def __init__(
            self, field_name: str = None, value: Union[Dict[str, Union[int, float]], Histogram] = None,
            default: Union[Dict[str, Union[int, float]], Histogram] = None,
            time_series_metric: Union[str, TimeSeriesMetric] = None
    ):
        super().__init__(
            field_name=field_name, value=value, default=default, time_series_metric=time_series_metric
        )
        return

    def __get__(
            self, instance: _Base, owner: Type["HistogramField"]
    ) -> Union["HistogramField", Union[Dict[str, Union[int, float]], Histogram]]:
        return super(HistogramField, self).__get__(instance=instance, owner=owner)

    def __set__(self, instance, value: Union[Dict[str, Union[int, float]], Histogram]):
        super(HistogramField, self).__set__(instance=instance, value=value)
        return

    @property
    def value(self) -> Union[Dict[str, Union[int, float]], Histogram]:
        return self._value

    @value.setter
    def value(self, value: Union[Dict[str, Union[int, float]], Histogram]):
        self._value: Union[Dict[str, Union[int, float]], Histogram] = value
        return

    @build_add_attr
    def _build(self) -> Dict:
        mapping: Dict = {
            "type": self._field_type
        }
        return mapping


